﻿using System;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using System.Linq;
using WFramework;
using WFramework.Common;
using WFramework.Extensions;
using WFramework.MvcPager;
using WFramework.Utility;

namespace MiniWeb.M.Controllers
{
    public class GoodsController : Controller
    {
        //
        // GET: /Goods/
        private readonly ProductsClassTask _productsClassTask = new ProductsClassTask();
        private readonly ProductsTypeTask _productsTypeTask = new ProductsTypeTask();
        private readonly ProductsInfoTask _productsInfoTask = new ProductsInfoTask();

        public ActionResult List(int? rid, int? cid, int? pl, int pageindex = 1, int pagesize = 16)
        {
            ViewData["typelist"] = _productsTypeTask.GetAll(100, "", null).ToSelectListForAll(c => c.ID.ToString(), c => c.TypeName);

            if (rid == null && cid != null)
            {

                var cmodel = _productsClassTask.GetById((int)cid);
                if (cmodel == null)
                {
                    return RedirectToAction("Index", "Home");
                }
                rid = cmodel.ProductsTypeID;
            }
            rid = rid ?? 1;
            ViewData["classlist"] = _productsClassTask.GetListByTID((int)rid);
            var typeinfo = new ProductsType();
            var classinfo = new ProductsClass();
            typeinfo = _productsTypeTask.GetById((int)rid);
            if (cid != null)
            {
                classinfo = _productsClassTask.GetById((int)cid);
            }
            ViewData["typeinfo"] = typeinfo;
            ViewData["classinfo"] = classinfo;



            var pagedList = _productsInfoTask.GetPagedList(null, rid, cid, null, true, false, false, false, null, null, pl, pageindex, pagesize);

            var model = new PagedList<ProductsInfoDto>(pagedList.ToList(), pageindex, pagesize, pagedList.TotalItemCount);

            return View(model);
        }

        public ActionResult Show(int id)
        {
            return View();
        }

        public ActionResult Search()
        {
            return View();
        }

        public ActionResult GetClass(int tid)
        {

            var list = _productsClassTask.GetAll(null, "ProductsTypeID = " + tid.ToString(), null).Select(c => new SelectListItem
            {
                Text = c.ClassName,
                Value = c.ID.ToString(),
            }).ToList();
            list.Insert(0, new SelectListItem
            {
                Selected = true,
                Text = "请选择",
                Value = "",
            });
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }
}
